package com.cdtye.itps.cms.service.datasysn;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.itps.cms.dao.detect.JcjcDefectGradeSection;
import com.cdtye.itps.cms.dao.enums.DelFlagEnum;
import com.cdtye.itps.cms.mapper.datasysn.JcjcDefectGradeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class JcjcDefectGradeService extends BaseService<JcjcDefectGradeMapper, JcjcDefectGradeSection> {
    @Autowired
    JcjcDefectGradeMapper jcjcDefectGradeMapper;

    @DS("gz")
    public JcjcDefectGradeSection findDefectGradeByIdGZ(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }

    @DS("sz")
    public JcjcDefectGradeSection findDefectGradeByIdSZ(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }
    @DS("cs")
    public JcjcDefectGradeSection findDefectGradeByIdCS(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }
    @DS("hy")
    public JcjcDefectGradeSection findDefectGradeByIdHY(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }
    @DS("hh")
    public JcjcDefectGradeSection findDefectGradeByIdHH(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }
    @DS("hk")
    public JcjcDefectGradeSection findDefectGradeByIdHK(String defectGradeId){
        return this.findDefectGradeByIdXX(defectGradeId);
    }

    public JcjcDefectGradeSection findDefectGradeByIdXX(String defectGradeId) {
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("defect_grade_id",defectGradeId);
        queryWrapper.and(wrapper  -> wrapper.eq("DEL_FLAG", DelFlagEnum.NO).or().isNull("DEL_FLAG"));
        return jcjcDefectGradeMapper.selectOne(queryWrapper);
    }



    @DS("gz")
    public List<JcjcDefectGradeSection> findLevelDateGZ(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }
    @DS("cs")
    public List<JcjcDefectGradeSection> findLevelDateCS(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }
    @DS("sz")
    public List<JcjcDefectGradeSection> findLevelDateSZ(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }
    @DS("hh")
    public List<JcjcDefectGradeSection> findLevelDateHH(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }
    @DS("hy")
    public List<JcjcDefectGradeSection> findLevelDateHY(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }
    @DS("hk")
    public List<JcjcDefectGradeSection> findLevelDateHK(Date date){
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date));
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }

    @DS("tmis")
    public List<JcjcDefectGradeSection> findDefectGradeListByParamsTmis(String defectGradeName) {
        QueryWrapper<JcjcDefectGradeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper  -> wrapper.eq("DEL_FLAG", DelFlagEnum.NO.value).or().isNull("DEL_FLAG"));
//        queryWrapper.isNotNull("LEVEL_CODE_GTJT");//段级库有重复等级的情况，取我们设置这个不为空的那一行
        if (StringUtils.isNotBlank(defectGradeName)) {
            queryWrapper.like("defect_grade_name",defectGradeName);
        }
        return jcjcDefectGradeMapper.selectList(queryWrapper);
    }

}
